package com.cqc.gridviewdemo01;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Created by ${cqc} on 2017/1/5.
 */

public class MyAdapter extends BaseAdapter {

    private String[] array;
    private int position;

    public void setPosition(int position) {
        this.position = position;
    }

    public MyAdapter(String[] array) {
        this.array = array;
    }

    @Override
    public int getCount() {
        return array.length;
    }

    @Override
    public Object getItem(int i) {
        return array[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(viewGroup.getContext(), R.layout.item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(array[i]);
        return convertView;
    }

    public class ViewHolder {
        TextView tv;

        public ViewHolder(View itemView) {
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
